package jdbc;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.*;


//JDBC 的使用
public class Insertjdbc {
    public  boolean Insert(String snid,String name,String author,String press) throws SQLException {

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/libarysystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        Connection connection = dataSource.getConnection();

        String sql = "insert into booklist (SNID ,name, author,press) values (?,?,?,?);";//内容待定
        //此处光是一个String 类型的sql还不行,要把这个String 包装成一个"语句对象"
        PreparedStatement statement = connection.prepareStatement(sql);
        //替换 sql 中 ? 的内容
        statement.setString(1,snid);
        statement.setString(2,name);
        statement.setString(3,author);
        statement.setString(4,press);

        System.out.println(statement);
        
        //4.执行SQL
        int ret = statement.executeUpdate();
        System.out.println(ret);

        //5. 此时 SQL 执行完毕, 然后还需要释放资源
        statement.close();
        connection.close();

        if (ret !=0){
            return true;
        }
        else {
            return false;
        }
    }
}